/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * <p>Project: app2022 - TryWithDemo
 * <p>Powered by webrx On 2022-01-18 09:54:49
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class TryWithDemo {
    public static void main(String[] args) {
        //try catch finally
        File file = new File("my.txt");
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            //fis.readAllBytes(); java 1.9 新增
            byte[] buf = fis.readAllBytes();
            System.out.println(new String(buf));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //try with
        try (FileInputStream f = new FileInputStream(file)) {
            byte[] buf = f.readAllBytes();
            System.out.println(new String(buf));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
